/* Consumption.ado (STATA)
	Estimate model of short-term rates.
	by Ralph Koijen & Motohiro Yogo */

#delimit ;

program Consumption;
args n ymax;


/* Load data */

u Estimation`n', clear;

/* Estimate reduced form */

qui reg Lmb $macro Icounterpar_*
	if year<=`ymax' & type==1 & Iycounter_euro, cluster(year);

eststo Lmb;

/* Estimate latent demand */

predict epsC if type==1 & Iycounter_euro, resid;

/* Save coefficients */

foreach var of varlist $macro {;
	gen bC_`var' = _b[`var'] if type==1;

	label var bC_`var' "Consumption coefficient: `var'";
};

gen bC_cons = _b[_cons] if type==1;

foreach var of varlist Icounterpar_* {;
	replace bC_cons = bC_cons+_b[`var']*`var' if type==1;
};

/* Output table */

esttab Lmb, b(%8.3f) se(%8.3f) plain
	drop(Icounterpar_*);

eststo clear;

/* Label variables */

label var epsC		"Consumption latent demand";
label var bC_cons	"Consumption coefficient: Constant";

/* Save data */

sort year country counterpart type;

save Consumption`n', replace;

end;
